#include "statisticalwidget.h"
#include "ui_statisticalwidget.h"


void StatisticalWidget::bar_init()
{
    barH = new QBarSet("正常");         //设置上方数据
    barH->setColor(mySkin[curr_skin].color_1);   //设置颜色
    barL = new QBarSet("异常");          //设置下方数据
    barL->setColor(mySkin[curr_skin].color_2);  //设置颜色
    *barL << -22 << -10 << -9.3 << -17.0 << -15.6 << -8.0
      << -6.0 << -11.8 << -9.7 << -12.8 << -23.0 << -28.0;
    *barH << 11.9 << 12.8 << 18.5 << 26.5 << 32.0 << 34.8
       << 38.2 << 34.8 << 29.8 << 20.4 << 15.1 << 11.8;

     series_bar = new QStackedBarSeries(this);
     //添加数据
     series_bar->append(barL);
     series_bar->append(barH);

     Chart_bar = new QChart();
     Chart_bar->addSeries(series_bar);
     QFont font_spline("Arial",16);
     Chart_bar->setTitleFont(font_spline);
     Chart_bar->setTitle("各部门出勤率");                         // 设置标题
     Chart_bar->setAnimationOptions(QChart::SeriesAnimations);  //开启动画效果
     Chart_bar->setAnimationDuration(1000);

     //设置X轴
     QBarCategoryAxis *axisX = new QBarCategoryAxis();
     QFont font_axisX("Arial",12);
     axisX->setTitleFont(font_axisX);
     QStringList strlist = {"1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12"};
     axisX->append(strlist);
     Chart_bar->addAxis(axisX,Qt::AlignBottom); //设置X坐标位置
     //设置Y轴
     QValueAxis *axisY = new QValueAxis();
     axisY->setRange(-100, 100);

     Chart_bar->addAxis(axisY,Qt::AlignLeft);   //设置Y坐标位置
     series_bar->attachAxis(axisX);
     series_bar->attachAxis(axisY);

     ui->graphicsView_bar->setRenderHint(QPainter::Antialiasing);// 设置抗锯齿
     ui->graphicsView_bar->setChart(Chart_bar);
}

/*更新数据*/
void StatisticalWidget::slot_bar_roll(void)
{
    barH->setColor(mySkin[curr_skin].color_1);   //设置颜色
    barL->setColor(mySkin[curr_skin].color_2);  //设置颜色

    for(int i  = 0;i < 12; i++)
    {
        barH->replace(i,qrand()%100);
        barL->replace(i,-qrand()%100);
    }
}


void StatisticalWidget::do_bar_roll(const QString &Title, const QMap<QString, double> &deptMap)
{
    // 1. 设置动态标题
    Chart_bar->setTitle(Title);

    // 2. 更新X轴部门名称
    QBarCategoryAxis *axisX = qobject_cast<QBarCategoryAxis*>(Chart_bar->axes(Qt::Horizontal).first());
    axisX->clear();
    QStringList deptNames = deptMap.keys();
    axisX->append(deptNames);

    // 3. 更新上下数据
    //重新添加数据
    for (int i = 0; i < deptNames.count(); ++i) {
        double attendance = deptMap.value(deptNames.at(i));
        barH->replace(i, attendance);
        barL->replace(i, -(100 - attendance));
    }

    // 4. 强制刷新图表
    Chart_bar->update();
}


